<?php
// no direct access
defined( '_AppMiage') or die( 'Restricted access');
if(isEnseignant ()){
    //include header
    include_once (DS . 'include/header.php');
    //include menu
    include_once (DS . 'include/menu.php');

    include_once(DS . 'include/zones.php');

    $showOK = false;
    $showError = false;


    if (isset($_POST['submit']))
    {
        // variable qui va nous servir pour savoir s'il y a a eu une erreur ;)
        $form = true;
        $errMSG = array();

        if (isset($_POST['name']) && !empty($_POST['name']))
        {
            $tmp = $global_member->setNomUtilisateur($_POST['name']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['nom'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['prenom']) && !empty($_POST['prenom']))
        {
            $tmp = $global_member->setPrenomUtilisateur($_POST['prenom']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['prenom'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['mail']) && !empty($_POST['mail']))
        {
            $tmp = $global_member->setEmailUtilisateur($_POST['mail']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['mail'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['old_pwd']) && !empty($_POST['old_pwd']) &&
                isset($_POST['pwd']) && !empty($_POST['pwd']) &&
                isset($_POST['pwd2']) && !empty($_POST['pwd2']))
        {
            if (md5($_POST['old_pwd']) == $global_member->getPass())
            {
                if ($_POST['pwd'] == $_POST['pwd2'])
                {
                    $tmp = $global_member->setPassword($_POST['pwd']);
                    if ($tmp['boolOk'] === false)
                    {
                        $errMSG['pwd'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                else
                {
                    $form = false;
                }
            }
            else
            {
                $form = false;
            }
        }

        if (isset($_POST['IG']) && !empty($_POST['IG']))
        {
            foreach ($_POST['IG'] as $IdTypeCritere => $arrayCriteres)
            {
                if ($IdTypeCritere == 1)
                {
                    foreach ($arrayCriteres as $IdPromo => $infos)
                    {
                        //echo '<script type="text/javascript">alert("type=' . $IdTypeCritere . ' ; idPromo=' . $IdPromo . ' ; nbEtudiantMax=' . $infos['NbEtudiantMax'] . ' ; idPromoCritere='. $infos['IdPromoCritere'] .'");</script>';
                        //echo '<script type="text/javascript">alert("promo");</script>';
                        //vardump($infos);
                        $global_member->addCriterePromo($IdPromo, $infos['NbEtudiantMax'], $infos['NbEtudiant'], $infos['IdPromoCritere']);
                    }
                }
                else
                {
                    foreach ($arrayCriteres as $IdCritere)
                    {
                        //echo '<script type="text/javascript">alert("type=' . $IdTypeCritere . ' ; id=' . $IdCritere . '");</script>';
                        $global_member->addCritere($IdTypeCritere, $IdCritere);
                    }
                }
            }
        }

        if( isset($_POST['message']) && !empty($_POST['message']) )
        {
            $tmp = $global_member->setMessage($_POST['message']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['message'] = $tmp['message'];
                $form = false;
            }
        }

        if ($form === true)
        {
            $global_member->update();
            if ($global_member->requeteOk === true)
            {
                $showOK = true;
    ?>
                <script language="javascript" type="text/javascript">
                    <!--
                    window.location.replace("<?php echo URL . 'index.php?m=profil_tuteur_enseignant'; ?>");
                    -->
                </script>
    <?php
            }
            else
            {
                $showError = true;
            }
        }
    }
    $lstPromo = Promo::getListPromo();
    $lstDomaine = DomaineApplication::getListDomaineApplication();
    $lstTechno = Technologie::getList();
    $lstCategorie = Categorie::getListCategory();
    $lstPromoCritere = PromoCritere::getListByIdEnseignant($global_member->getIdUtilisateur());
    //vardump($global_member);
    //vardump($lstPromoCritere);
    ?>
    <div id="content">
        <div class="post">
            <div class="entry">
                <h1 class="title">Modifier mon Profil</h1>
                <?php if ($showError): ?>
                    <div class="error">Une erreur est survenue. Veuillez re-essayer ult&egrave;rieurement.</div>
                <?php endif; ?>
                <?php if ($showOK): ?>
                        <div class="success">La sauvegarde s'est correctement effectu&eacute;e.</div>
                <?php endif; ?>
                    </div>
                    <form action="<?php echo URL; ?>index.php?m=profil_tuteur_enseignant" method="post" id="custom">
                        <fieldset title="Mes Informations">
                            <legend> </legend>
                            <label for="name">Nom : </label><br />
                            <input class="title" type="text" name="name" id="name" value="<?php echo $global_member->getNomUtilisateur(); ?>"/><br />
                    <?php
                        if (isset($errMSG['nom']) && !empty($errMSG['nom']))
                        {
                    ?><p class="error"><?php echo $errMSG['nom']; ?></p><?php } ?>
                        <br />
                        <label for="prenom">Pr&eacute;nom : </label><br />
                        <input class="title" type="text" name="prenom" id="prenom" value="<?php echo $global_member->getPrenomUtilisateur(); ?>"/><br />
                    <?php
                        if (isset($errMSG['prenom']) && !empty($errMSG['prenom']))
                        {
                    ?><p class="error"><?php echo $errMSG['prenom']; ?></p><?php } ?>
                        <br />
                        <label for="mail">Email : </label><br />
                        <input class="title" type="text" name="mail" id="mail" value="<?php echo $global_member->getEmailUtilisateur(); ?>"/><br />
                    <?php
                        if (isset($errMSG['mail']) && !empty($errMSG['mail']))
                        {
                    ?><p class="error"><?php echo $errMSG['mail']; ?></p><?php } ?>
                        <br />
                        <label for="old_pwd">Ancien mot de passe : </label><br />
                        <input class="title" type="password" name="old_pwd" id="old_pwd" value=""/><br />
                        <br />
                        <label for="pwd">Mot de passe : </label><span style="font-size:10px;">entre 6 et 15 caract&egrave;res</span><br />
                        <input class="title" type="password" name="pwd" id="pwd" value="" onkeyup="verifLength(this.value,'length');" />&nbsp;&nbsp;<span id="length"></span><br />
                        <br />
                        <label for="pwd2">Retaper le mot de passe : </label><br />
                        <input class="title" type="password" name="pwd2" id="pwd2" value="" onkeyup="verifPass($('#pwd').val(),$('#pwd2').val() , 'result');" />&nbsp;&nbsp;<span id="result"></span><br />
                        <br />
                        <br />
                    </fieldset>
                    <fieldset title="Mes criteres">
                        <legend> </legend>
                        <label for="promo">Promo : </label>
                        <table>
                        <?php
                        $i = 0;
                        foreach ($lstPromo as $idPromo => $e)
                        {
                            if ($i == 0)
                            {
                                echo "<tr>";
                            }
                        ?>

                            <td>
                                <label for="IG[promo-<?php echo $idPromo; ?>]" style="font-weight:normal;cursor:pointer;">
                                <?php echo $e; ?>
                            </label>
                        </td>
                        <?php
                                $cle = $lstPromoCritere[$idPromo];
                                if (!empty($cle))
                                {
                        ?>
                                    <td><input type="checkbox"
                            <?php
                                    echo 'checked';
                            ?> name="IG[1][<?php echo $idPromo; ?>][IdPromoCritere]" value="<?php echo $cle['IdPromoCritere']; ?>" id="checkPromo-<?php echo $idPromo; ?>" onclick="checkPromo(<?php echo $idPromo; ?>, this);"/>
                         </td>
                         <td>
                             <label for="nbEtu">Nombre d&rsquo;&eacute;tudiants que je souhaite encadrer :</label>
                         </td>
                         <td>
                             <input type="text" id="nbEtudiantMax-<?php echo $idPromo; ?>" name="IG[1][<?php echo $idPromo; ?>][NbEtudiantMax]" value="<?php echo $cle['NbEtudiantMax']; ?>"/><br/>
                             <input type="hidden" id="nbEtudiant-<?php echo $idPromo; ?>" name="IG[1][<?php echo $idPromo; ?>][NbEtudiant]" value="<?php echo $cle['NbEtudiant']; ?>"/>
                         </td>
                        <?php
                                }
                                else
                                {
                        ?>
                                    <td>
                                        <input id="checkPromo-<?php echo $idPromo; ?>" onclick="checkPromo(<?php echo $idPromo; ?>, this);" type="checkbox" name="IG[1][<?php echo $idPromo; ?>][IdPromoCritere]" value="0" />
                                    </td>
                                    <td>
                                        <label for="">Nombre d&rsquo;&eacute;tudiants que je souhaite encadrer :</label>
                                    </td>
                                    <td>
                                        <input type="text" id="nbEtudiantMax-<?php echo $idPromo; ?>" name="" value=""/><br/>
                                        <input type="hidden" id="nbEtudiant-<?php echo $idPromo; ?>" name="" value=""/>
                                    </td>
                                    <script type="text/javascript">
                                        function checkPromo(promo, check)
                                        {
                                            var idInputPromo = "nbEtudiantMax-" + promo;
                                            var idInputHidden = "nbEtudiant-" + promo;
                                            if( check.checked )
                                            {
                                                document.getElementById(idInputPromo).setAttribute("name", "IG[1][" + promo + "]" + "[NbEtudiantMax]");
                                                document.getElementById(idInputPromo).setAttribute("value", "1");
                                                document.getElementById(idInputHidden).setAttribute("name", "IG[1][" + promo + "]" + "[NbEtudiant]");
                                                document.getElementById(idInputHidden).setAttribute("value", "0");
                                            }
                                            else
                                            {
                                                document.getElementById(idInputPromo).removeAttribute("name");
                                                document.getElementById(idInputPromo).setAttribute("value", "");
                                                document.getElementById(idInputHidden).removeAttribute("name");
                                                document.getElementById(idInputHidden).setAttribute("value", "");
                                            }
                                        }
                                    </script>
                        <?php
                                }
                        ?>
                        <?php
                                if ($i == 0)
                                {
                                    echo "</tr>";
                                    $i = 0;
                                }
                                else
                                {
                                    $i++;
                                }
                            }
                        ?>
                        </table>
                        <label for="">Lieux :</label>
                        <table>
                        <?php
                            foreach ($zones as $idZone => $zone)
                            {
                                $str = "<b>" . $zone['nom'] . "</b><br/>(<i>";
                                $nbVille = 1;
                                foreach ($zone['ville'] as $idVille => $ville)
                                {
                                    if ($nbVille == count($zone['ville']))
                                    {
                                        $str .= $ville;
                                    }
                                    else
                                    {
                                        $str .= $ville . ", ";
                                    }
                                    $nbVille += 1;
                                }
                                $str .= "</i>)";
                        ?>
                                <tr>
                                    <td>
                                        <label for="" style="font-weight:normal;cursor:pointer;"><?php echo $str; ?></label>
                                    </td>
                                    <td>
                                        <input <?php
                                if ($global_member->hasChooseThisCritere(2, $idZone))
                                {
                                    echo 'checked';
                                }
                        ?> type="checkbox" name="IG[2][<?php echo $idZone; ?>]" value="<?php echo $idZone; ?>" />
                            </td>
                        </tr>
                        <?php
                                }
                        ?>

                                <tr>
                                    <td>
                                        <label for="" style="font-weight:normal;cursor:pointer;"><b>Autre d&eacute;partement</b></label>
                                    </td>
                                    <td>
                                        <input <?php
                                if ($global_member->hasChooseThisCritere(2, 0))
                                {
                                    echo 'checked';
                                }
                        ?> type="checkbox" name="IG[2][0]" value="0" />
                            </td>
                        </tr>
                        <tr>
                                    <td>
                                        <label for="" style="font-weight:normal;cursor:pointer;"><b>Etranger</b></label>
                                    </td>
                                    <td>
                                        <input <?php
                                if ($global_member->hasChooseThisCritere(2, 12))
                                {
                                    echo 'checked';
                                }
                        ?> type="checkbox" name="IG[2][0]" value="12" />
                            </td>
                        </tr>
                    </table>
                    <label for="IG[techno]">Technologie  : </label><br />
                    <table cellspacing="0">
                        <?php
                                $i = 0;
                                foreach ($lstTechno as $k => $e)
                                {
                                    if ($i == 0)
                                    {
                                        echo "<tr>";
                                    }
                        ?>

                                    <td style="border-left:1px #999 solid;">
                                        <label for="IG[techno-<?php echo $e->idTechnologie; ?>]" style="font-weight:normal;cursor:pointer;">
                                <?php echo $e->NomTechnologie; ?>
                                </label>
                            </td>
                            <td style="border-right:1px #999 solid;"><input <?php
                                    if ($global_member->hasChooseThisCritere(3, $e->idTechnologie))
                                    {
                                        echo 'checked';
                                    } ?> type="checkbox" name="IG[3][<?php echo $e->idTechnologie; ?>]" value="<?php echo $e->idTechnologie; ?>" /></td>
                            <?php
                                    if ($i == 3)
                                    {
                                        echo "</tr>";
                                        $i = 0;
                                    }
                                    else
                                    {
                                        $i++;
                                    }
                                }
                            ?>
                        </table>
                        <br />
                        <label for="Categorie">Cat&eacute;gorie :</label><br />
                        <table>
                        <?php
                                $i = 0;
                                foreach ($lstCategorie as $k => $e)
                                {
                                    if ($i == 0)
                                    {
                                        echo "<tr>";
                                    }
                        ?>

                                    <td>
                                        <label for="IG[cat-<?php echo $e->IdCategorie; ?>]" style="font-weight:normal;cursor:pointer;">
                                <?php echo $e->NomCategorie; ?>
                                </label>
                            </td>
                            <td><input type="checkbox" <?php
                                    if ($global_member->hasChooseThisCritere(4, $e->IdCategorie))
                                    {
                                        echo 'checked';
                                    } ?> name="IG[4][<?php echo $e->IdCategorie; ?>]" value="<?php echo $e->IdCategorie; ?>" /></td>
                            <?php
                                    if ($i == 1)
                                    {
                                        echo "</tr>";
                                        $i = 0;
                                    }
                                    else
                                    {
                                        $i++;
                                    }
                                }
                            ?>
                        </table>
                        <br />
                        <label for="Domaine">Domaine :</label><br />
                        <table>
                        <?php
                                $i = 0;
                                foreach ($lstDomaine as $k => $e)
                                {
                                    if ($i == 0)
                                    {
                                        echo "<tr>";
                                    }
                        ?>

                                    <td>
                                        <label for="IG[dom-<?php echo $e->idDomaineApplication; ?>]" style="font-weight:normal;cursor:pointer;">
                                <?php echo $e->NomDomaineApplication; ?>
                                </label>
                            </td>
                            <td><input type="checkbox" <?php
                                    if ($global_member->hasChooseThisCritere(5, $e->idDomaineApplication))
                                    {
                                        echo 'checked';
                                    } ?> name="IG[5][<?php echo $e->idDomaineApplication; ?>]" value="<?php echo $e->idDomaineApplication; ?>" /></td>
                            <?php
                                    if ($i == 1)
                                    {
                                        echo "</tr>";
                                        $i = 0;
                                    }
                                    else
                                    {
                                        $i++;
                                    }
                                }
                            ?>
                        </table>
                    </fieldset>
                    <fieldset title="Message personnel">
                        <legend> </legend>
                        <label for="message">Laisser un message personnel au responsable des stages : </label>
                        <br />
                        <textarea name="message"><?php echo stripcslashes($global_member->getMessage()); ?></textarea><br />
                    </fieldset>
                    <input type="hidden" value="submit" name="submit"/>
                    <div align="center">
                        <input type="submit" class="button green" disable="disable" value="Sauver" name="submit">
                    </div>
                </form>
            </div>
        </div>
    <?php
	
    include_once (DS. 'include/sidebar.php');
    include_once (DS. 'include/footer.php');
	
}
else
{
    redirectTo();
}
?>